import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        collectList: [], // 收藏数据
        active: true,
    },

    form: {
        page: 1, //页码
        size: 10, //条数
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.collectPost(); //收藏数据
    },
    async collectPost() {
        let res = await request({ url: "Collect/findAll", data: this.form });
        if (res.code == 200) {
            if (res.data.records.length == 0) {
                this.setData({
                    active: false,
                });
            } else {
                this.setData({
                    collectList: res.data.records,
                    active: true,
                });
            }
        }
        console.log(res);
    },
    // 普通商品收藏按钮
    async collect(e) {
        if (wx.getStorageSync("token")) {
            let res = await request({
                url: "Collect/update",
                data: {
                    goodId: e.target.dataset.id,
                    type: 1,
                },
            });
            console.log(res);
            if (res.code == 200) {
                this.collectPost(); //商品详情数据
            }
        } else {
            wx.switchTab({
                url: "../myName/index",
            });
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    //积分商品收藏
    async collection(e) {
        if (wx.getStorageSync("token")) {
            let res = await request({
                url: "Collect/update",
                data: {
                    goodId: e.target.dataset.id,
                    type: 2,
                },
            });
            console.log(res);
            if (res.code == 200) {
                this.collectPost(); //商品详情数据
            }
        } else {
            wx.switchTab({
                url: "../myName/index",
            });
            wx.showToast({
                title: "请先登录",
                icon: "none",
            });
        }
    },
    generalGoodsBtn(e) {
        console.log(e);
        wx.navigateTo({
            url: "../commodity_details/commodity_details?goodsId=" +
                e.currentTarget.dataset.id,
        });
    },
    integralGoodsBtn(e) {
        console.log(e);
        wx.navigateTo({
            url: "../pointProduct/pointProduct?goodsIntegralId=" +
                e.currentTarget.dataset.id,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});